home *** CD-ROM | disk | FTP | other *** search
- /******************************************************/
- /* COMM.H - this set of routines creates a */
- /* interupt driven communications */
- /* interface for MSC 5.0 & quickC. */
- /* By Mario Giannini */
- /* */
- /* */
- /* setintr(port) - install driver for com port */
- /* port where 0 = com1: */
- /* returns -1 if port not installed */
- /* */
- /* resetint(port) - remove above driver */
- /* */
- /* seravl() - tests to see if a character is */
- /* available in the serial buffer */
- /* */
- /* getser() - gets a character from the serial */
- /* buffer */
- /* */
- /* portinit(port, baud, parity, data, stop) */
- /* returns 0 if ok */
- /* -1 if port not there, */
- /* -2 if invalid parameter */
- /* */
- /* is_carrier(port) - returns a non-zero if */
- /* carrier detect is true */
- /* */
- /* */
- /* sendstr(str) - transmits a string to port, */
- /* translates a '|' into CR, and */
- /* */
- /* xmit(ch) - transmit character ch */
- /* */
- /* clr_buf() - clears out buffer */
- /* */
- /* onesec() - one second time delay */
- /* */
- /* hwhangup() - hangup modem through DTR line */
- /* gets port from variable init'ed by */
- /* portinit */
- /******************************************************/
-
- #ifndef BUFSIZE
- #define BUFSIZE 8192
- #endif
- #define MAXPORTS 2
- #define seravl() (tailp!=headp)
- #define keyavl() kbhit()
-
- int _int_port; /* 1 for COM1, 2 for COM2, etc... */
-
- /****************************************/
- /* serial interupt data & declarations */
- /****************************************/
-
-
- struct comport {
- int inport[MAXPORTS]; /* input port address of UART */
- int outport[MAXPORTS]; /* output port "" */
- int baudl[MAXPORTS]; /* baud rate divisor high and low bytes */
- int baudh[MAXPORTS];
- int inten[MAXPORTS]; /* interupt enable port */
- int intid[MAXPORTS]; /* interupt id port */
- int lcreg[MAXPORTS]; /* line control register */
- int mcreg[MAXPORTS]; /* modem control reg */
- int lstatus[MAXPORTS]; /* line status reg */
- int mstatus[MAXPORTS]; /* modem status reg */
- int intmsk[MAXPORTS]; /* mask for com port interupt */
- int intr[MAXPORTS];
- } comx = {
- {0x3f8, 0x2f8},
- {0x3f8, 0x2f8},
- {0x3f8, 0x2f8},
- {0x3f9, 0x2f9},
- {0x3f9, 0x2f9},
- {0x3fa, 0x2fa},
- {0x3fb, 0x2fb},
- {0x3fc, 0x2fc},
- {0x3fd, 0x2fd},
- {0x3fe, 0x2fe},
- {0xef, 0xf7},
- {0x0c, 0x0b}
- };
- /* column 1 is com1, column 2 is com2, etc.. */
-
-
- volatile char _icbuffer[BUFSIZE]; /* buffer storage, head & tails pointers */
- volatile char *headp, *tailp, *buf_end, *buf_beg;
-
- void (_CDECL interrupt far *serial)(); /* for origninal serial int. */
-
- void _CDECL interrupt far handler(void);
-
- /****************************************/
- /* The actual serial interface driver */
- /****************************************/
-
- void _CDECL interrupt far handler()
- {
- int dummy; /* this is to allow for a bug in MSC 5.0 that may
- delete code when optimizing */
- _disable();
-
- *tailp++=inp(comx.inport[_int_port]);
- if (tailp==buf_end)
- tailp=buf_beg;
- if (tailp==headp) {
- headp++;
- if (headp==buf_end)
- headp=buf_beg;
- }
- _enable();
- outp(0x20, 0x20); /* send an end of interupt signal to hardare */
-
- _chain_intr(serial);
- }
- /******END OF SERIAL INTERUPT***************/
-
-
- /*****************************************/
- /* SET & RESET SERIAL INTERUPT */
- /*****************************************/
-
- setintr(int cport)
- {
- unsigned i;
- buf_beg=_icbuffer;
- buf_end=&_icbuffer[BUFSIZE];
- tailp=buf_beg;
- headp= buf_beg;
- if (inp(comx.inport[cport])==0xFF && inp(comx.lstatus[cport])==0xFF && inp(comx.lcreg[cport])==0xFF)
- return(-1); /* no UART found at port address */
-
- /* install the handler */
- _int_port=cport;
- serial=_dos_getvect(comx.intr[cport]);
- _dos_setvect(comx.intr[cport], handler); /* point to our handler */
-
- /* set up the interupt controller */
-
- outp(comx.mcreg[cport], 0xB); /* set OUT2, DTR, & RTS on UART */
- outp(comx.inten[cport], 1); /* set interupt enable reg on UART to data ready */
- i=inp(0x21); /* setup interupt controller chip */
- i= (i & comx.intmsk[cport]);
- outp(0x21, i);
- return(0);
- }
-
-
- resetintr(int cport)
- {
- int i,j;
- /* first disable the interupts via interrupt mask on PIC chip */
-
- i=~comx.intmsk[cport];
- j=inp(0x21);
- j=j | i;
- /* outp(0x21, j); */
-
- /* then restore the original serial intr pointer */
-
- _dos_setvect(comx.intr[cport], serial);
- }
-
- /*******************************************/
- /* recieve to COM? functions */
- /*******************************************/
-
- getser()
- {
- int ch;
-
- ch=*headp++;
- if (headp==buf_end)
- headp=buf_beg;
- return(ch);
- }
-
-
- /******************************************/
- /* func to init COM? to correct settings */
- /* returns a -1 if port not there, */
- /* -2 if invalid parameter */
- /* 0 if ok */
- /******************************************/
-
- portinit(cport, baud, parity, data, stop)
- int cport, baud, parity, data, stop;
- {
-
- unsigned char attrib=0;
- char temp;
- int cbaud;
-
-
- /* these items setup the attribute bits for the byte to be sent
- to the UART (data bits, stop bits, etc.) */
-
- if (inp(comx.inport[cport])==0xFF && inp(comx.lstatus[cport])==0xFF && inp(comx.lcreg[cport])==0xFF)
- return(-1); /* no UART found at port address */
-
-
- if (cport>MAXPORTS || parity>2 || data>8 || stop>2)
- return(-2);
- if (parity != 0) {
- if (parity==1)
- attrib=8;
- else
- attrib=24;
- }
- if (stop>1)
- attrib=attrib+4;
- temp=data-5;
- attrib=attrib+temp;
- cbaud=0;
- if (baud==9600)
- cbaud=0xC;
- else
- if (baud==4800)
- cbaud=0x18;
- else
- if (baud==2400)
- cbaud=0x30;
- else
- if (baud==1200)
- cbaud=0x60;
- else
- if (baud==300)
- cbaud=0x180;
- else
- return(-2);
-
- outp(comx.lcreg[cport], (unsigned) 0x80);
- outp(comx.baudl[cport], cbaud%0x100);
- outp(comx.baudh[cport], cbaud/0x100);
- outp(comx.lcreg[cport], attrib);
-
- return(0);
-
- }
-
- is_carrier(int cport)
- {
- int i;
- i=inp(comx.mstatus[cport]);
- #ifdef DEBUG
- return(0x80);
- #else
- return(i&0x80);
- #endif
- }
-
- clr_buf() /* clear communications buffer */
- {
- tailp=buf_beg;
- headp=tailp;
- }
-
-
- xmit(char ch)
- {
- #ifdef DEBUG
- putch(ch);
- #else
- while ( ((inp(comx.lstatus[_int_port])) & 0x20 ) ==0);
- outp(comx.outport[_int_port], ch);
- #endif
- }
-
- sendstr(char *str)
- {
- while (*str) {
- if (*str == '|')
- xmit(13);
- else if (*str=='~') {
- onesec();
- }
- else
- xmit(*str);
- str++;
- }
-
- }
-
- onesec()
- {
- union REGS regin, regout;
-
- unsigned int dest;
-
- regin.h.ah=0;
- int86(0x1A, ®in, ®out);
- dest=regout.x.dx+18;
- while( (unsigned) regout.x.dx < dest) {
- regin.h.ah=0;
- int86( 0x1A, ®in, ®out);
- }
- }
-
- hwhangup()
- {
- int i;
- i=inp(comx.mcreg[_int_port]);
- i=i&0xFE;
- outp(comx.mcreg[_int_port], 0); /* all bits off, then on, then normal */
- inp(comx.mcreg[_int_port]);
- outp(comx.mcreg[_int_port], 0xFF);
- inp(comx.mcreg[_int_port]);
- outp(comx.mcreg[_int_port], i);
- onesec();
- i=i|1;
- outp(comx.mcreg[_int_port], i);
- }